//
//  FXTHomeViewController.m
//  OfferJob
//
//  Created by 郑州动力无限科技 on 2017/6/30.
//  Copyright © 2017年 郑州动力无限科技. All rights reserved.
//

#import "FXTHomeViewController.h"
#import "SendTableViewCell.h"
#import "ADCollectionViewCell.h"
#import "SDCycleScrollView.h"
#import "KindScrolleViewCell.h"
#import "lunboView.h"
#import "CityListViewController.h"
#import "newsViewController.h"
#import "SearchViewController.h"
#import "sendDetailViewController.h"

@interface FXTHomeViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate,KindScrolleViewCellDelegate,BannerCollectionReusableViewDelegate,CityListViewDelegate,UITextFieldDelegate>
@property (nonatomic,strong)UITableView* tableView;
@property (nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property (nonatomic,strong) NSMutableArray *courseTypeArray;
@property (nonatomic,strong) NSMutableArray *bannerArray;
@property (nonatomic,strong) NSMutableArray *listarray;
@property (nonatomic,strong)  lunboView*luobo;
@property (nonatomic,strong)  NSMutableDictionary*dic;
@property (nonatomic,strong) NSMutableArray *list;
@property (nonatomic,strong) NSString *imageStr;
@property (nonatomic,strong)NSMutableArray * TypeArr;
@property (nonatomic,strong)NSMutableArray * Typee;
@end

@implementation FXTHomeViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [super viewWillAppear:animated];
    
}
-(void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}
-(NSMutableArray *)courseTypeArray{
    if (!_courseTypeArray) {
        _courseTypeArray = [[NSMutableArray alloc] init];
    }
    return _courseTypeArray;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
//     style:UITableViewStyleGrouped
    _listarray=[[NSMutableArray alloc]init];
    _bannerArray=[[NSMutableArray alloc]init];
    _dic=[[NSMutableDictionary alloc]init];
    _TypeArr=[[NSMutableArray alloc]init];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-49)];
//    [self setHeadView];
    [self getUserInfo];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor=backgroundcolor;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView registerClass:[SendTableViewCell class] forCellReuseIdentifier:@"SendTableViewCell"];
    [self.view addSubview:_tableView];
  
}

// 获取
-(void)getUserInfo{
    FXTHttpRequestModel * requestModel = [[FXTHttpRequestModel alloc] init];
    requestModel.type = FXTHttpType_POST;
    requestModel.appendUrl = @"/Task/tasklist";
    NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
    [paramDic setValue:@"郑州" forKey:@"city"];
    requestModel.paramDic = paramDic;
    requestModel.callback = ^(FXTHttpResponseModel * result, NSError *error){
        [self.bannerArray removeAllObjects];
        if (result.state ==FXTReponseCodeState_Success) {
            NSLog(@"请求的结果是%@",result.data);
            self.imageStr=[result.data  objectForKey:@"ad_img"];
      
            for (NSDictionary* dic in [result.data  objectForKey:@"banner"]) {
             
                [self.bannerArray  addObject:[dic  objectForKey:@"banner_img"]];
            }
           self.TypeArr=[result.data  objectForKey:@"type"];
    
            
           [self setHeadView];
            
            self.listarray=[result.data  objectForKey:@"list"] ;
//                         NSLog(@"_listdic_listdic果是%@", self.listarray);
            

            [_tableView reloadData];
            
        }else {
            [DisplayHelper displayWarningAlert:result.alertMsg];
        }
    };
    [[FXTHttpSessionManager sharedHttpSessionManager] sendHttpRequestParam:requestModel];
}

-(void)setHeadView
{
    UIView *viewHeader=[[UIView alloc]init];

    //轮播图
    float aa= 0*kAppScale;
    
    
   _luobo=[[lunboView  alloc]initWithFrame:CGRectMake(0,0*kAppScale, SCREEN_WIDTH, 150*kAppScale)];
    _luobo.bannerImageArray = self.bannerArray;
    _luobo.delegate = self;
    [viewHeader addSubview:_luobo];
  
    [self getZuiHead];
    
    aa+=_luobo.bottom;
    
    KindScrolleViewCell* cell=[KindScrolleViewCell new];
    CGRect frame = cell.frame;
    frame.origin.y = 150;
    cell.frame = frame;
    cell.height=200*kAppScale;
    cell.width=SCREEN_WIDTH;
    cell.backgroundColor = [UIColor whiteColor];
    cell.kindArray = self.TypeArr;
    cell.delegate = self;
    [viewHeader  addSubview:cell];
    
    

    UIImageView* viewa=[[UIImageView alloc]initWithFrame:CGRectMake(0,cell.bottom,SCREEN_WIDTH, 100*kAppScale)];
    [viewa setImageWithURL:[NSURL  URLWithString:self.imageStr] placeholderImage:[UIImage imageNamed:@""]];
    [viewHeader addSubview:viewa];
    
    UIImageView* viewjing=[[UIImageView alloc]initWithFrame:CGRectMake(10,viewa.bottom+7*kAppScale,5*kAppScale, 30*kAppScale)];
    viewjing.backgroundColor=[UIColor blueColor];
    [viewHeader addSubview:viewjing];
    
    
    UILabel*labletitle=[[UILabel alloc]initWithFrame:CGRectMake(viewjing.right+5*kAppScale, viewa.bottom, SCREEN_WIDTH, 44*kAppScale)];
    labletitle.text=@"每日精品";
    labletitle.textColor=[UIColor blackColor];
    labletitle.font=[UIFont systemFontOfSize:14*kAppScale];
    [viewHeader  addSubview:labletitle];
    

  viewHeader.height=labletitle.bottom;
  [_tableView setTableHeaderView:viewHeader];

    
}
#pragma mark - UITextFieldDelegate

//头部的搜索框
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.hidesBottomBarWhenPushed=YES;
    SearchViewController *searchVc=[[SearchViewController alloc]initWithBlock:^(NSString *key) {
        if (key && key.length>0) {
            
            
        }
    }];
    [self.navigationController pushViewController:searchVc animated:YES];
    self.hidesBottomBarWhenPushed=NO;
    return NO;
}

- (void)getZuiHead{

    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0,18, SCREEN_WIDTH, 35)];
    view.backgroundColor=[UIColor clearColor];
    [_luobo  addSubview:view];
    
    
    
    UIButton *  tempBt=[UIButton buttonWithType:UIButtonTypeCustom];

    tempBt.frame = CGRectMake(30*kAppScale, -5*kAppScale,50*kAppScale, 20*kAppScale);
    tempBt.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:16.0f];
//    [tempBtn  setImage:[UIImage imageNamed:@"icon_dsz"] forState:UIControlStateNormal];
    [tempBt setTitle:@"郑州" forState:UIControlStateNormal];
     [tempBt setImage:[UIImage imageNamed:@"icon_jto"] forState:UIControlStateNormal];
       [tempBt TiaoZhengButtonWithOffsit:5 TextImageSite:UIButtonTextLeft];
    [tempBt addTarget:self action:@selector(locationUsers:) forControlEvents:UIControlEventTouchUpInside];
    [tempBt setTitleColor:blackTextColor forState:UIControlStateNormal];
    [view addSubview:tempBt];
    
    UIImageView *SearchBG=[[UIImageView alloc]initWithFrame:CGRectMake(tempBt.right+10*kAppScale, -5, 220*kAppScale,32*kAppScale)];
    SearchBG.layer.borderColor = [[UIColor whiteColor] CGColor];
    SearchBG.layer.borderWidth = 0.5;
    SearchBG.layer.cornerRadius = 3;
    SearchBG.backgroundColor=whiteLineColore;
    SearchBG.userInteractionEnabled=YES;
    [view addSubview:SearchBG];
    
    UIImageView *IconImage=[[UIImageView alloc]initWithFrame:CGRectMake(8*kAppScale, -1*kAppScale, 25*kAppScale, 25*kAppScale)];
    IconImage.image=[UIImage imageNamed:@"search"];
    [SearchBG addSubview:IconImage];
    UITextField *searchText=[[UITextField alloc]initWithFrame:CGRectMake(IconImage.right+10*kAppScale, 0, SearchBG.width-SearchBG.height-5*kAppScale , SearchBG.height)];
    searchText.font=[UIFont systemFontOfSize:13*kAppScale];
    searchText.placeholder=@"输入关键字搜索";
    [searchText setValue:grayTextColor forKeyPath:@"_placeholderLabel.textColor"];
    searchText.tag=666;
    searchText.textColor=blackTextColor;
    searchText.delegate=self;
    [SearchBG addSubview:searchText];
    
    
    
    UIButton *  tempBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [tempBtn setAdjustsImageWhenHighlighted:NO];
    tempBtn.frame = CGRectMake(SearchBG.right+30*kAppScale, -5*kAppScale, 20*kAppScale, 20*kAppScale);
    tempBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:16.0f];
    [tempBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [tempBtn  setImage:[UIImage imageNamed:@"icon_dsz"] forState:UIControlStateNormal];
    [tempBtn addTarget:self action:@selector(registerUsers) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:tempBtn];

}
- (void)locationUsers:(UIButton*)btn{
    CityListViewController *cityListView = [[CityListViewController alloc]init];
    cityListView.title = @"选择城市";
    cityListView.delegate = self;
    //热门城市列表
    cityListView.arrayHotCity = [NSMutableArray arrayWithObjects:@"郑州市",@"广州市",@"北京市",@"天津市",@"上海市",@"深圳市",@"长沙市",@"武汉市", nil];
    //定位城市列表
    NSString * currentCity = @"";
//    if (self.tempPlacemark.addressDictionary[@"City"]!=nil) {
//        currentCity = self.tempPlacemark.addressDictionary[@"City"];
//    }
//    else
    {
        currentCity = @"未定位";
    }
    cityListView.arrayLocatingCity   = [NSMutableArray arrayWithObjects:currentCity, nil];
    
    [self.navigationController pushViewController:cityListView animated:YES];

}
- (void)registerUsers{
    newsViewController* new=[[newsViewController alloc]init];
    [self .navigationController pushViewController:new animated:YES];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listarray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.listarray.count>0) {
    _dic=self.listarray[indexPath.row];
            NSLog(@"_dic_dic_dic%@",_dic);
    }
    
    SendTableViewCell *cell = (SendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SendTableViewCell" forIndexPath:indexPath];
    cell.imageView.backgroundColor=[UIColor redColor];
 cell.nameLB.text=[NSString stringWithFormat:@"%@",_dic[@"nickname"]];
    CGSize lblSize = [cell.nameLB.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 30*kAppScale) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    cell.nameLB.frame = CGRectMake(80*kAppScale, 30*kAppScale, lblSize.width, lblSize.height);
      cell.iconImage.frame=CGRectMake( cell.nameLB.right+5*kAppScale, 30*kAppScale,15*kAppScale,15*kAppScale);
       cell.iconImage.image=[UIImage imageNamed:@"icon_bao"];

    [cell .imagView setImageWithURL:[NSURL URLWithString:    [NSString stringWithFormat:@"%@",_dic[@"header_pic"]]] placeholderImage:[UIImage imageNamed:@""]];
    cell.titleLB.text=[NSString stringWithFormat:@"%@",_dic[@"title"]];
    cell.numLB.text= [NSString stringWithFormat:@"人数：%@人",_dic[@"num"]];
    if ([_dic[@"jiesuan_type"]isEqualToString:@"1"]) {
      cell.dayLB.text=@"日结";
    }else if ([_dic[@"jiesuan_type"]isEqualToString:@"2"]){
    cell.dayLB.text=@"小时结";
    }else if ([_dic[@"jiesuan_type"]isEqualToString:@"3"]){
        cell.dayLB.text=@"次结";
    }else if ([_dic[@"jiesuan_type"]isEqualToString:@"4"]){
      cell.dayLB.text=@"完工结";
    }
    
    NSString* dayname;
    if ([[_dic objectForKey:@"jiesuan_unit"]isEqualToString:@"1"]) {
        dayname=@"天";
    }else if ([[_dic objectForKey:@"jiesuan_unit"]isEqualToString:@"2"]) {
        dayname=@"小时";
    }else  if ([[_dic objectForKey:@"jiesuan_unit"]isEqualToString:@"3"]){
        dayname=@"次";
    }else{
        dayname=@"预算";
    }
    cell. moneyLB.attributedText=[[NSString stringWithFormat:@"<red14>%@元</red14><heise12>/%@</heise12>",_dic[@"jiesuan_price"],dayname]attributedStringWithStyleBook:[self Style]];
    cell.timeLB.text=[NSString stringWithFormat:@"%@-%@", [NSString stringWithFormat:@"%@",_dic[@"date_starttime"]], [NSString stringWithFormat:@"%@",_dic[@"date_endtime"]]];
   
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    sendDetailViewController*sendDetailVC=[[sendDetailViewController alloc]init];
    sendDetailVC.strID=self.listarray[indexPath.row][@"task_id"];
    NSLog(@"sendDetailVC.strID=单是%@", sendDetailVC.strID);
    [self.navigationController pushViewController:sendDetailVC animated:YES];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80*kAppScale;
}




-(FXTNavType)getNavType {
    return FXTNavTypeNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
